﻿// CDelUserDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "SaleSystem.h"
#include "CDelUserDlg.h"


// CDelUserDlg

IMPLEMENT_DYNCREATE(CDelUserDlg, CFormView)

CDelUserDlg::CDelUserDlg()
	: CFormView(DIALOG_DELUSER)
	, m_pwd(_T(""))
	, m_contrl(0)
{

}

CDelUserDlg::~CDelUserDlg()
{
}

void CDelUserDlg::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combo);
	DDX_Text(pDX, IDC_EDIT1, m_pwd);
	DDX_Text(pDX, IDC_EDIT2, m_contrl);
}

BEGIN_MESSAGE_MAP(CDelUserDlg, CFormView)
	ON_BN_CLICKED(IDC_BUTTON1, &CDelUserDlg::OnBnClickedButton1)
	ON_CBN_SELCHANGE(IDC_COMBO1, &CDelUserDlg::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDC_BUTTON2, &CDelUserDlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CDelUserDlg 诊断

#ifdef _DEBUG
void CDelUserDlg::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CDelUserDlg::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDelUserDlg 消息处理程序


void CDelUserDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	//读取文件，获取用户名，给组合框添加字符串
	CInfoFile file;
	file.ReadUserline(); //读取用户信息
	for (list<l_user>::iterator it = file.l_ls.begin(); it != file.l_ls.end(); it++)
	{
		m_combo.AddString((CString)it->name.c_str());
	}


	//将第一个用户名设为默认选中项
	m_combo.SetCurSel(0);
}


void CDelUserDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//获取控件上的内容，更新到对应关联的变量中
	UpdateData(TRUE);


	CString type;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取组合框当前内容
	m_combo.GetLBText(index, type);

	CString str;
	str.Format(_T("注销用户：%s \r\n密码：%s \r\n权限：%d "), type, m_pwd, m_contrl);
	MessageBox(str);


	CInfoFile file;
	file.ReadUserline(); //读取用户信息
	for (list<l_user>::iterator it = file.l_ls.begin(); it != file.l_ls.end(); it++)
	{
		if (type == it->name.c_str())
		{
			//it->control = it->control - control;
			/*it->name.Empty();

			m_name.Empty();*/
			it->control = 0;
		}
	}
	file.WriteUserline(); //更新文件内容

	UpdateData(FALSE); //更新到对应的控件
}


void CDelUserDlg::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString text;
	//获取当前选中项
	int index = m_combo.GetCurSel();
	//获取当前内容
	m_combo.GetLBText(index, text);

	CInfoFile file;
	file.ReadUserline(); //读取用户信息
	for (list<l_user>::iterator it = file.l_ls.begin(); it != file.l_ls.end(); it++)
	{
		if (text == it->name.c_str())
		{
			m_pwd = it->l_pwd.c_str();
			m_contrl = it->control;
			UpdateData(FALSE); //内容更新到对应的控件
		}
	}
}


void CDelUserDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	m_combo.SetCurSel(0); //选择第0项目
	OnCbnSelchangeCombo1();
}
